package com.queclink.ycm.entity.data;

import com.alibaba.fastjson.JSONObject;
import com.queclink.ycm.common.utils.HexUtils;
import com.queclink.ycm.common.utils.StringUtil;

/**
 * @author jim.li
 * @email jim.li@queclink.com
 * @date 2021-12-24 17:30:00
 */
public class Data85 extends Data {

    private String PLMN;
    private String LAC;
    private String CellID;
    private String Roaming;
    private String AccessTechnology;
    private String Band;
    private String CSQ;
    private Integer RSSI;
    private Integer BitErrorRate;

    private String plmnLength;


    public Data85() {
    }

    public String getPLMN(JSONObject jsonObject) {
        Integer plmn = jsonObject.getInteger("PLMN");
        String plmnStr = HexUtils.intToHex(plmn, 6);
//        Integer plmnLength = jsonObject.getInteger("plmnLength");
//        String plmnLengthStr = HexUtils.intToHex(plmnLength, 2);
        String plmnLengthStr = "00";
        return plmnLengthStr + plmnStr;
    }

    public String getLAC(JSONObject jsonObject) {
        String lac = jsonObject.getString("LAC");
        if (lac.length() > 2) {
            lac = lac.substring(0, 2);
        }
        String str = StringUtil.padZeroesToTheLeft(lac, 2);
        return HexUtils.string2HexUTF8(str);
    }

    public String getCellID(JSONObject jsonObject) {
        String cellID = jsonObject.getString("CellID");
        if (cellID.length() > 4) {
            cellID = cellID.substring(0, 4);
        }
        String str = StringUtil.padZeroesToTheLeft(cellID, 4);
        return HexUtils.string2HexUTF8(str);
    }



    public String getAccessTechnologyAndRoming(JSONObject jsonObject) {
        Integer accessTechnology = jsonObject.getInteger("AccessTechnology");
        Integer roaming = jsonObject.getInteger("Roaming");
        Integer total = roaming * 128 + accessTechnology;
        String str = HexUtils.intToHex(total, 2);
        return str;

    }

    public String getBand(JSONObject jsonObject) {
        Integer band = jsonObject.getInteger("Band");
        return HexUtils.intToHex(band, 2);
    }

    public String getCSQRSSI(JSONObject jsonObject) {
        JSONObject csq = jsonObject.getJSONObject("CSQ");
        Integer rssi = csq.getInteger("RSSI");
        return HexUtils.intToHex(rssi, 2);
    }

    public String getBitErrorRate(JSONObject jsonObject) {
        JSONObject csq = jsonObject.getJSONObject("CSQ");
        Integer bitErrorRate = csq.getInteger("BitErrorRate");
        return HexUtils.intToHex(bitErrorRate, 2);
    }

    public String getDataID() {
        // data85
        return HexUtils.intToHex(85, 2);
    }

    public String getLength() {
        return "0E";
    }

    @Override
    public String combine(JSONObject jsonObject) throws Exception {
        StringBuffer sb = new StringBuffer();
        sb.append(this.getDataID());
        sb.append(this.getLength());
        sb.append(this.getPLMN(jsonObject));
        sb.append(this.getLAC(jsonObject));
        sb.append(this.getCellID(jsonObject));
        sb.append(this.getAccessTechnologyAndRoming(jsonObject));
        sb.append(this.getBand(jsonObject));
        sb.append(this.getCSQRSSI(jsonObject));
        sb.append(this.getBitErrorRate(jsonObject));
        return sb.toString();
    }

}
